<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>收件人新增</title>
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">收件人邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" lay-verify="required|email" autocomplete="off"
                           placeholder="请输入收件人邮箱"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">收件人姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required|name" autocomplete="off"
                           placeholder="请输入收件人姓名"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">收件人电话</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" lay-verify="required|phone" autocomplete="off"
                           placeholder="请输入收件人电话"
                           class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn  layui-btn-sm" lay-submit=""
                    lay-filter="recipient-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:inline="javascript">

    layui.use(['form', 'jquery'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let form = layui.form;
        let layer = layui.layer;
        let $ = layui.$;
        // 自定义校验规则
        form.verify({
            // 验证收件人姓名，且为必填项
            name: function (value) {
                if (value.length < 1 || value.length > 20) {
                    return '收件人姓名长度应在1到50个字符之间';
                }
            },
            // 验证收件人电话，且为必填项
            phone: function (value) {
                if (!/^\d{11}$/.test(value)) {
                    return '收件人电话必须是11位数字';
                }
            }
        })
        form.on('submit(recipient-save)', function (data) {
            let groupDate = {
                id: data.field.id,
                email: data.field.email,
                phone: data.field.phone,
                name: data.field.name,
                groupId: parent.parent.id
            }
            $.ajax({
                url: baseURL + 'cms/recipient/add',
                data: JSON.stringify(groupDate),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code === 1000) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name)); //关闭当前页
                            parent.layui.table.reload("recipient-table");
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 2000
                        }, function () {
                            parent.layui.table.reload("recipient-table");
                        });
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>